// 导入数据库模块
const db = require('../DB/index');
// 导入获取时间的包
const date = require('silly-datetime')

// 查询所有用户数据
exports.getAccount = (req,res) => {
    const sql = 'SELECT * FROM user'
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'获取用户成功',
            data:result
        })
    })
}

// 根据搜索内容查询用户数据
exports.searchDate = (req,res)=>{
    let sql = `SELECT * FROM user WHERE 1 = 1 `
    const obj = req.body
    if(obj.searchUserName !== ''){
        sql += `AND username LIKE '%${obj.searchUserName}%'  `
    }
    if(obj.searchName !== ''){
        sql += `AND name LIKE '${obj.searchName}%' `
    }
    if(obj.searchRole !== ''){
        sql += `AND role LIKE '%${obj.searchRole}%' `
    }
    if(obj.searchAddress !== ''){
        sql += `AND address LIKE '%${obj.searchAddress}%' `
    }
    console.log(sql);
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'搜索成功',
            data:result
        })
    })
 
}
// 添加用户处理函数
exports.addUser =(req,res)=>{
    // 获取客户端提交到的用户信息
    const userinfo = req.body
    // 定义sql语句,判断注册用户名是否已存在
    const sql = `select * from user where username = ?`
    db.query(sql,[userinfo.username],(err,result)=>{
        if(err) return res.cc(err)
        // 判断用户名是否被占用
        if(result.length > 0){
            return res.cc('请更换其他用户名')
        }
        const sql = `insert into user set ?`
        // 获取创建时间
        const createDates = date.format(new Date(),'YYYY-MM-DD HH:mm:ss')  
        db.query(sql,
            {username:userinfo.username,
            password:userinfo.password,
            name:userinfo.name,
            phone:userinfo.phone,
            address:userinfo.address,
            role:userinfo.role,
            town_id:userinfo.town_id,
            attendance_id:userinfo.attendance_id,
            approver_id:userinfo.approver_id,
            createDate:createDates
        },(err,result)=>{
            // 判断sql语句是否执行成功
            if(err) return res.cc(err)
            // 判断影响函数是否为1
            if(result.affectedRows !== 1) return res.cc('注册用户失败，请稍后再试！')
            res.cc('新增成功' ,0)
        })  
    })
}
//修改用户
exports.updateUser = (req,res)=>{
    const data = req.body
    const sql = `UPDATE user SET username='${data.username}',password='${data.password}', name = '${data.name}', phone = '${data.phone}', address = '${data.address}', role = '${data.role}',
    attendance_id='${data.attendance_id}' ,approver_id='${data.approver_id}',town_id = ${data.town_id} where id='${data.id}'`
 
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('修改数据失败')
        res.cc('修改成功',0)
    })
}
// 删除用户
exports.delUser = (req,res)=>{
    const id = req.body.id
    const sql = `DELETE FROM user WHERE id=${id};`
    db.query(sql,id,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('删除数据失败')
        res.cc('删除数据成功',0)
    })
}
// 查看所有审批员信息
exports.lookApprover = (req,res)=>{
    const sql = `SELECT * FROM user WHERE role = 2`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.length < 0) return res.cc('查询失败')
        res.send({
            status:0,
            data:result
        })
    })
}
